// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
    env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate
// 复习本查询 
exports.main = async (event, context) => {
    if (event.type == 'review') {
        const res = await db.collection('reviewList').aggregate()
            // .match(event.openid)
            .match({
                _openid: _.eq(event.openid)
            })
            .group({
                // _id必要字段名。其下依次根据year、mon、day分类
                _id: {
                    year: '$year',
                    mon: '$mon',
                    day: '$day',
                    flag: '$flag'
                },
                // 将每个类别下的content字段值赋值到con字段名，组成object，然后将整个object放到li字段名上
                newArr: $.addToSet({
                    con: '$newArr',
                    // id:'$newArr.id'
                })
            })
            .sort({
                "_id.year":-1,
                "_id.mon":-1,
                "_id.day":-1
            })
            .limit(1000)
            .end()
        return res
    } else if (event.type == 'master') {
        const res = await db.collection('masterList').aggregate()
            .match({
                openid: _.eq(event.openid)
            })
            .group({
                // _id必要字段名。其下依次根据year、mon、day分类
                _id: {
                    year: '$year',
                    mon: '$mon',
                    day: '$day'
                },
                // 将每个类别下的content字段值赋值到con字段名，组成object，然后将整个object放到li字段名上
                newArr: $.addToSet({
                    con: '$newArr',
                })
            })
            .limit(1000)
            .end()
        return res
    }
}